﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class NoiDungHienThi_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public NoiDungHienThi_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<NoiDungHienThi> getListBy(string whereCom, string orderCom)
        {
            List<NoiDungHienThi> data = new List<NoiDungHienThi>();
            string comStr = "Select * from [NoiDungHienThi]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        NoiDungHienThi obj = new NoiDungHienThi();

                        obj.MaNoiDung = dap.DaReader["MaNoiDung"].ToString();
                        obj.TenNoiDung = dap.DaReader["TenNoiDung"].ToString();
                        obj.NoiDung = dap.DaReader["NoiDung"].ToString();
                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }
        public List<NoiDungHienThi> getListByKey()
        {
            return getListBy("", " [MaNoiDung] asc");
        }

        private NoiDungHienThi getBy(string whereCom, string orderCom)
        {
            NoiDungHienThi data = null;
            string comStr = "Select * from [NoiDungHienThi]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                if (dap.HasData)
                {
                    data = new NoiDungHienThi();
                    dap.DaReader.Read();

                    data.MaNoiDung = dap.DaReader["MaNoiDung"].ToString();
                    data.TenNoiDung = dap.DaReader["TenNoiDung"].ToString();
                    data.NoiDung = dap.DaReader["NoiDung"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public NoiDungHienThi getByKey(string MaNoiDung)
        {
            return getBy(" [MaNoiDung]='" + MaNoiDung + "' ", "");
        }

        private int update(NoiDungHienThi obj, string whereCom)
        {

            string comQuery = "UPDATE [NoiDungHienThi] SET [MaNoiDung]=@MaNoiDung,[TenNoiDung]=@TenNoiDung"
               + ",[NoiDung]=@NoiDung ";
            if (whereCom.Trim() != "")
                comQuery += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaNoiDung", obj.MaNoiDung, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@TenNoiDung", obj.TenNoiDung, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@NoiDung", obj.NoiDung, System.Data.SqlDbType.NVarChar);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int updateByKey(NoiDungHienThi data, string MaNoiDung)
        {
            return update(data, " [MaNoiDung]='" + MaNoiDung + "' ");
        }


    }
}